package by.dvrudenko.avtobaza.webapp.page.driver.panel;

import javax.inject.Inject;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.Url;
import org.apache.wicket.request.resource.ContextRelativeResource;
import org.apache.wicket.request.resource.UrlResourceReference;

import by.dvrudenko.avtobaza.datamodel.Driver;
import by.dvrudenko.avtobaza.datamodel.Route;
import by.dvrudenko.avtobaza.datamodel.User;
import by.dvrudenko.avtobaza.services.DriverService;
import by.dvrudenko.avtobaza.webapp.page.AbstractPanel;
import by.dvrudenko.avtobaza.webapp.page.createDriver.CreateDriverPage;
import by.dvrudenko.avtobaza.webapp.page.createRoute.CreateRoutePage;
import by.dvrudenko.avtobaza.webapp.page.driver.DriverPage;
import by.dvrudenko.avtobaza.webapp.page.route.page.RoutePage;
import by.dvrudenko.avtobaza.webapp.utils.security.SecuredLinkForAdmin;
import by.dvrudenko.avtobaza.webapp.utils.security.SecuredLinkForDriver;

public class DriverDetailsPanel extends AbstractPanel {
	
	@Inject
	private DriverService driverService;
	private Driver driver;

	public DriverDetailsPanel(String id, Driver driver) {
		super(id);
		this.driver = driver;
	}

	@Override
	protected void onInitialize() {
		super.onInitialize();
		
		WebMarkupContainer divContainer = new WebMarkupContainer("wrapper-div");
		WebMarkupContainer liContainer = new WebMarkupContainer("wrapper-li");
		add(liContainer);
		WebMarkupContainer hContaiener = new WebMarkupContainer("wrapper-h");
		hContaiener.add(new Label("panelDriverTitle", getResourceModel("p.home.driver.panel.driverPanelTitle")));
		liContainer.add(hContaiener);
		divContainer.add(liContainer);
		add(divContainer);
		
		// добавляем картинку
		final String urlStr = getImageDriver(driver);
        final UrlResourceReference url= new UrlResourceReference(Url.parse(urlStr));
        liContainer.add(new Image("wrapper-img", url));
		
        
		liContainer.add(new Label("driverLastName", new Model<String>(driver.getLastName())));
		liContainer.add(new Label("driverName", new Model<String>(driver.getUser().getName())));		
		liContainer.add(new Label("driverBirthday", new Model<Integer>(getYearFromDate(driver.getAge()))));
		liContainer.add(new Label("driverExperience", new Model<Integer>(getYearFromDate(driver.getExperience()))));
		liContainer.add(new Label("driverAcomplishRoutes", new Model<Integer>(driver.getAcomplishRoutes())));
		
		liContainer.add(new Link<Void>("driverPage") {
			@Override
			public void onClick() {
				setResponsePage(DriverPage.class);
			};
		});
		
		Link<Void> createDriver = new SecuredLinkForAdmin("createDriver"){
			@Override
			public void onClick() {
				Driver driver= new Driver();
				User user = new User();
				
				setResponsePage(new CreateDriverPage(driver,user));
			};
		};	
		liContainer.add(createDriver);
		
		
		Link<Void> viewRoute = new SecuredLinkForDriver("viewRoute"){
			@Override
			public void onClick() {
				setResponsePage(RoutePage.class);
			};
		};	
		liContainer.add(viewRoute);		

	}


}
